package com.baizhi.cmfzmanager.controller;

import com.baizhi.cmfzmanager.entity.Article;
import com.baizhi.cmfzmanager.lucene.LuceneService;
import com.baizhi.cmfzmanager.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/lucene")
public class LuceneController {

    @Autowired
    private LuceneService luceneService;
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/queryByKeyword")
    public Map queryByKeyword(int page,int rows,String keyword){
        if(keyword!=null&&!"".equals(keyword)){
            List<Article> articles = luceneService.queryByKeyword(keyword);
            Map map = new HashMap();
            map.put("total",articles.size());
            map.put("rows",articles);

            return map;
        }
        return articleService.getArticles(page,rows);
    }

    @RequestMapping("/resetIndexDB")
    public Map resetIndexDB(){
        luceneService.resetIndexDB();
        Map map = new HashMap();
        map.put("message","重置成功");
        return map;
    }

}
